package ExceptionDemo;

import java.util.ArrayList;
import java.util.List;

public class ExceptionDemo2 {

    public static void main(String[] args) throws Exception {
        //testE();
        //使用捕获异常方式
/*        try{
            testE2();
        }catch (Exception e){
            System.out.println(e.toString());
        }
        System.out.println("hahaha");*/
        printAge(-1);

        System.out.println("我是张家辉");
    }

    public static void printAge(int age) throws Exception{
        if (age < 0 || age > 100){
            System.out.println("年龄不合法");
        }else {
            System.out.println("恭喜年龄合法");
        }
    }



    public static void testE(){
        //空指针异常
        StringBuffer sb = null;
        sb.append("a");//NullPointerException

    }

    public static void testE2(){
        //角标越界异常
        List<String> list = new ArrayList<>();
        list.add("哈哈哈");
        System.out.println(list.get(2));
    }
}
